<template>
  <div class='questions-container'>
    <el-card>
      <questions-pagetools/>
      <questions-form @search='hSearch'/>
      <el-alert
      :title="alertText"
      type="info"
      class="alert"
      :closable="false"
      show-icon
      ></el-alert>
      <!-- 表格 -->
      <el-table
      :header-cell-style="{background:'#fafafa'}"
      :data="testList"
      style="width: 100%">
        <el-table-column
          prop="number"
          label="试题编号"
          width="120">
        </el-table-column>
        <el-table-column
          prop="subject"
          label="学科"
          width="105">

        </el-table-column>
        <el-table-column
          prop="catalogID"
          label="目录"
          width="100">
          <template #default="{row}">
           {{formatDirectory(row.catalogID)}}
          </template>
        </el-table-column>
        <el-table-column

          label="题型"
          width="105">
          <template #default="{row}">
           {{formatQuestionType(row.questionType)}}
          </template>
        </el-table-column>
        <el-table-column
          label="题干"
          width="280">
          <template #default="{row}" >
            <span v-html="row.question"></span>
          </template>
        </el-table-column>
        <el-table-column
          label="录入时间"
          width="180"
          >
          <template #default="{row}">
           {{formatDate(row.addDate)}}
          </template>
        </el-table-column>
        <el-table-column

          label="难度"
          width="100">
          <template #default="{row}">
           {{formatDifficulty(row.difficulty)}}
          </template>
        </el-table-column>
        <el-table-column
          prop="creator"
          label="录入人"
          width="105">
        </el-table-column>
        <el-table-column
          label="操作"
          >
          <template #default="{row}">
            <el-button size="small" plain type="primary" icon="el-icon-view" circle @click="hPreview(row)" title="预览"></el-button>
            <el-button size="small" plain type="success" icon="el-icon-edit" circle @click="$router.push(`/questions/new?id=${row.id}`)" title="修改"></el-button>
            <el-button size="small" plain type="danger" icon="el-icon-delete" circle @click="hDel(row.id)" title="删除"></el-button>
            <el-button size="small" plain type="warning" icon="el-icon-check" circle @click="isSelect(row)" title="加入精选"></el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-row type="flex" justify="end" class="pagination">
        <el-pagination
          @size-change="handleSizeChange"
          background
          @current-change="handleCurrentChange"
          :current-page="q.page"
          :page-sizes="[5, 10, 20, 50]"
          :page-size="q.pagesize"
          layout="prev,pager,next,sizes,jumper"
          :total="total">
        </el-pagination>
      </el-row>
    </el-card>
   <el-dialog title="题目预览" :visible.sync="showDialog" width="900px">
      <questions-preview :curId="curId" v-if="showDialog"></questions-preview>
   <span slot="footer" class="dialog-footer" >
    <div class="close">
      <el-button @click="showDialog = false"  type="primary">关闭</el-button>
    </div>
  </span>
   </el-dialog>
  </div>

</template>

<script>
import moment from 'moment'
import * as questions from '@/api/hmmm/questions.js'
// import { formatDate } from '@/utils'
import { difficulty, questionType } from '@/api/hmmm/constants.js'
import * as directorys from '@/api/hmmm/directorys.js'

const difficultyMnus = {}
difficulty.forEach(item => {
  difficultyMnus[item.value] = item.label
})
const questionTypeMnus = {}
questionType.forEach(item => {
  questionTypeMnus[item.value] = item.label
})

export default {
  data () {
    return {
      alertText: '共10条数据',
      testList: [],
      q: {
        page: 1,
        pagesize: 10
      },
      total: 1,
      directory: null,
      showDialog: false,
      curId: '',
      directoryList: {}
    }
  },
  async created () {
    this.directoryList = await this.loadlist()
  },
  mounted () {
    this.questionType = questionType
    this.difficulty = difficultyMnus
    console.log(difficulty)
  },
  methods: {
    async  isSelect (row) {
      if (row.isChoice) {
        row.isChoice = 0
      } else {
        row.isChoice = 1
      }
      this.$confirm('确认加入精选?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'info'
      }).then(async () => {
        const res = await questions.choiceAdd({ id: row.id, choiceState: row.isChoice })
        if (res.status === 200) {
          this.$message({
            type: 'success',
            message: '加入成功!'
          })
          this.loadlist()
        }
      }).catch((error) => {
        this.$message.error(error)
      })
    },
    hDel (id) {
      this.$confirm('确认删除该题目?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const res = await questions.remove({ id })
        if (res.status === 200) {
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
          this.loadlist()
        }
      }).catch((error) => {
        this.$message.error(error)
      })
    },
    hPreview (row) {
      this.curId = row.id
      this.showDialog = true
    },
    async  hSearch (form) {
      console.log(form)
      this.q = { ...this.q, ...form }
      Object.keys(this.q).forEach(key => {
        if (this.q[key] === '') {
          delete this.q[key]
        }
      })
      await this.loadlist()
      Object.keys(this.q).forEach(key => {
        if (key === 'page' || key === 'pagesize') {

        } else {
          delete this.q[key]
        }
      })
    },
    handleSizeChange (val) {
      // console.log(`每页 ${val} 条`)
      this.q.pagesize = val
      this.q.page = 1
      this.loadlist()
    },
    handleCurrentChange (val) {
      // console.log(`当前页: ${val}`)
      this.q.page = val
      this.loadlist()
    },
    async  loadlist () {
      try {
        const res = await questions.list(this.q)
        console.log(res, 'list')
        if (res.status !== 200) return this.$message.error('获取失败')
        // this.$message.success('获取基础题库列表成功')
        const catalogList = await directorys.list({ page: 1, pagesize: 100 })
        console.log(catalogList)
        const directory = {}
        catalogList.data.items.forEach(item => {
          const { id, directoryName } = item
          directory[id] = directoryName
        })
        this.testList = res.data.items
        console.log(this.testList, ' this.testList')
        this.total = res.data.counts
        this.alertText = `共${res.data.counts}条数据`
        return directory
      } catch (error) {
        console.log(error)
      }
    },
    formatDate (date) {
      return moment(date).format('YYYY-MM-DD HH:mm:ss')
    },

    formatDifficulty (id) {
      return difficultyMnus[id] || '未知'
    },
    formatQuestionType (id) {
      return questionTypeMnus[id] || '未知'
    },
    formatDirectory (id) {
      return this.directoryList[id]
    }

  }

}
</script>

<style lang="less" scoped>
.questions-container{
    padding: 0 10px;
    margin: 10px 0;
    table thead{
      background-color: pink;
    }

  .alert {
  margin: 10px 0px;
  }
  .pagination{
    margin: 20px 0;
  }
  .close{
    display: flex;
    justify-content: flex-end;
  }
}
</style>
